﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace CardPlay.Client.Controls
{
    public class BlockGauge : Control
    {
        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(int), typeof(BlockGauge), 
            new FrameworkPropertyMetadata(-1, FrameworkPropertyMetadataOptions.AffectsRender));

        public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register("Minimum", typeof(int), typeof(BlockGauge),
            new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsRender));

        public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register("Maximum", typeof(int), typeof(BlockGauge),
            new FrameworkPropertyMetadata(10, FrameworkPropertyMetadataOptions.AffectsRender));

        public static readonly DependencyProperty GapWidthProperty = DependencyProperty.Register("GapWidth", typeof(int), typeof(BlockGauge),
            new FrameworkPropertyMetadata(2, FrameworkPropertyMetadataOptions.AffectsRender));

        public static readonly DependencyProperty StartColorProperty = DependencyProperty.Register("StartColor", typeof(Color), typeof(BlockGauge),
            new FrameworkPropertyMetadata(Colors.GreenYellow, FrameworkPropertyMetadataOptions.AffectsRender));

        public static readonly DependencyProperty EndColorProperty = DependencyProperty.Register("EndColor", typeof(Color), typeof(BlockGauge),
            new FrameworkPropertyMetadata(Colors.OrangeRed, FrameworkPropertyMetadataOptions.AffectsRender));

        public static readonly DependencyProperty StrokeProperty = DependencyProperty.Register("Stroke", typeof(Pen), typeof(BlockGauge),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));

        public int Value
        {
            get { return (int) GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        public int Minimum
        {
            get { return (int)GetValue(MinimumProperty); }
            set { SetValue(MinimumProperty, value); }
        }

        public int Maximum
        {
            get { return (int)GetValue(MaximumProperty); }
            set { SetValue(MaximumProperty, value); }
        }

        public int GapWidth
        {
            get { return (int)GetValue(GapWidthProperty); }
            set { SetValue(GapWidthProperty, value); }
        }

        public Color StartColor
        {
            get { return (Color)GetValue(StartColorProperty); }
            set { SetValue(StartColorProperty, value); }
        }

        public Color EndColor
        {
            get { return (Color)GetValue(EndColorProperty); }
            set { SetValue(EndColorProperty, value); }
        }

        public Pen Stroke
        {
            get { return (Pen)GetValue(StrokeProperty); }
            set { SetValue(StrokeProperty, value); }
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);

            var blockCount = Maximum - Minimum;
            if (blockCount <= 0 || Value <= Minimum) return;

            var blockWidth = (ActualWidth - (blockCount - 1) * GapWidth) / blockCount;

            var startColor = StartColor;
            var endColor = EndColor;

            var colorStep = Color.FromArgb(
                (byte)((endColor.A - startColor.A) / blockCount),
                (byte)((endColor.R - startColor.R) / blockCount),
                (byte)((endColor.G - startColor.G) / blockCount),
                (byte)((endColor.B - startColor.B) / blockCount));

            var bounds = new Rect(0, 0, blockWidth, ActualHeight);

            for (var i = 0; i < Value - Minimum; ++i)
            {
                var color = Color.FromArgb(
                    (byte)(startColor.A + colorStep.A * i),
                    (byte)(startColor.R + colorStep.R * i),
                    (byte)(startColor.G + colorStep.G * i),
                    (byte)(startColor.B + colorStep.B * i));

                drawingContext.DrawRectangle(new SolidColorBrush(color), Stroke, bounds);
                bounds.Offset(blockWidth + GapWidth, 0);
            }
        }
    }
}
